#!groovy

def branch = "master"
def test_tags = "${env.TEST_TAGS}".split(',')
def all_cypress_tags = "${env.CYPRESS_TAGS}".split('\\|')
if ("${env.branch}" != "null" && "${env.branch}" != "") {
  branch = "${env.branch}"
}

if ("${env.CORRAL_PACKAGES_REPO}" != "null" && "${env.CORRAL_PACKAGES_REPO}" != "") {
      corralRepo = "${env.CORRAL_PACKAGES_REPO}"
}

if ("${env.CORRAL_PACKAGES_BRANCH}" != "null" && "${env.CORRAL_PACKAGES_BRANCH}" != "") {
      corralBranch = "${env.CORRAL_PACKAGES_BRANCH}"
}

node {
  wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm', 'defaultFg': 2, 'defaultBg':1]) {
    withFolderProperties {
      paramsMap = []
        params.each {
          paramsMap << "$it.key=$it.value"
      }
        withEnv(paramsMap) {
            stage('Checkout') {
            deleteDir()
            checkout([
                        $class: 'GitSCM',
                        branches: [[name: "*/${branch}"]],
                        extensions: scm.extensions + [[$class: 'CleanCheckout']],
                        userRemoteConfigs: scm.userRemoteConfigs
                    ])
            }
            try {
                stage('Run Tests') {
                    jobs = [:]
                    for( String val : test_tags ) {
                        for ( String ct : all_cypress_tags ) {
                            params = null
                            params = [  string(name: 'JOB_TYPE', value: "${JOB_TYPE}"),
                                        string(name: 'CORRAL_PACKAGES_BRANCH', value: "${CORRAL_PACKAGES_BRANCH}"),
                                        string(name: 'CORRAL_PACKAGES_REPO', value: "${CORRAL_PACKAGES_REPO}"),
                                        text(name: 'CORRAL_PACKAGE_CONFIG', value: "${CORRAL_PACKAGE_CONFIG}"),
                                        string(name: 'CORRAL_VERSION', value: "${CORRAL_VERSION}"),
                                        string(name: 'AWS_SSH_USER', value: "${AWS_SSH_USER}"),
                                        string(name: 'AWS_AMI', value: "${AWS_AMI}"),
                                        string(name: 'AWS_REGION', value: "${AWS_REGION}"),
                                        string(name: 'AWS_SUBNET', value: "${AWS_SUBNET}"),
                                        string(name: 'AWS_VPC', value: "${AWS_VPC}"),
                                        string(name: 'AWS_INSTANCE_TYPE', value: "${AWS_INSTANCE_TYPE}"),
                                        string(name: 'AWS_VOLUME_TYPE', value: "${AWS_VOLUME_TYPE}"),
                                        string(name: 'AWS_VOLUME_IOPS', value: "${AWS_VOLUME_IOPS}"),
                                        string(name: 'AWS_VOLUME_SIZE', value: "${AWS_VOLUME_SIZE}"),
                                        string(name: 'AWS_ROUTE53_ZONE', value: "${AWS_ROUTE53_ZONE}"),
                                        string(name: 'NODEJS_VERSION', value: "${NODEJS_VERSION}"),
                                        string(name: 'DASHBOARD_REPO', value: "${DASHBOARD_REPO}"),
                                        string(name: 'DASHBOARD_BRANCH', value: "${DASHBOARD_BRANCH}"),
                                        string(name: 'CYPRESS_TAGS', value: ct),
                                        string(name: 'RANCHER_HOST', value: ""),
                                        string(name: 'RANCHER_USERNAME', value: "${RANCHER_USERNAME}"),
                                        string(name: 'RANCHER_PASSWORD', value: "${RANCHER_PASSWORD}"),
                                        string(name: 'K3S_KUBERNETES_VERSION', value: "${K3S_KUBERNETES_VERSION}"),
                                        string(name: 'CERT_MANAGER_VERSION', value: "${CERT_MANAGER_VERSION}"),
                                        string(name: 'SERVER_COUNT', value: "${SERVER_COUNT}"),
                                        string(name: 'AGENT_COUNT', value: "${AGENT_COUNT}"),
                                        string(name: 'BOOTSTRAP_PASSWORD', value: "${BOOTSTRAP_PASSWORD}"),
                                        string(name: 'BRANCH', value: "${BRANCH}"),
                                        string(name: 'CYPRESS_VERSION', value: "${CYPRESS_VERSION}"),
                                        string(name: 'YARN_VERSION', value: "${YARN_VERSION}"),
                                        string(name: 'CHROME_VERSION', value: "${CHROME_VERSION}"),
                                        string(name: 'QASE_PROJECT', value: "${QASE_PROJECT}"),
                                        string(name: 'QASE_REPORT', value: "${QASE_REPORT}"),
                                        string(name: 'RANCHER_IMAGE_TAG', value: val)]
                            echo "TAG: ${params}"
                            build (job: 'ui-automation-job', parameters: params, propagate: false, wait: false)
                        }
                        
                    }
                }
            } catch (err) {
                echo "Error: " + err
                currentBuild.result = 'FAILURE'
                error()
            }
        }
    }
  }
}
